<?php


namespace App\Models\Admin;


use Illuminate\Support\Facades\Redis;

class AdminRegion extends Base
{
    protected $table = 'admin_region';
    protected $guarded = [];
    public $timestamps = false;

    public static function getListByPid($pid)
    {
        $pid=$pid?$pid:100000;
        $where[]=['pid',$pid];
        $list = self::where($where)
            ->select(self::getField())
            ->get();
        return $list;
    }

    public static function getField()
    {
        return['id','name','pid','sname','level','citycode','yzcode','mername','lng','lat','pinyin'];
    }

    public static function getPathListById($id)
    {
        //获取路径
        $path_arr=self::getPathById($id);
        //获取省数据
        $list_1=self::getListByPid(0);
        //获取市数据
        $pid_2=end($path_arr);
        array_pop($path_arr);
        $list_2=self::getListByPid($pid_2);
        //获取区数据
        $pid_3=end($path_arr);
        array_pop($path_arr);
        $list_3=self::getListByPid($pid_3);
        $data_res=[
            'list_1'=>$list_1,
            'list_2'=>$list_2,
            'list_3'=>$list_3,
        ];
        return $data_res;
    }

    public static function getPathById($id)
    {
        $path_arr=array();
        do{
            $pid=self::where('id',$id)->value('pid');
            $id=$pid;
            $path_arr[]=$pid;
        }
        while($id!=100000);
        array_pop($path_arr);
        return $path_arr;
    }

    //根据父id，返回所有的县级id
    public static function getCountyIdsByPid($pid,$level=1)
    {
        $city_ids_arr=[];
        if($level==1)
        {
            $city_ids_arr=self::where('pid',$pid)->select('id')->get()
            ->transform(function ($i,$k)
            {
                $i=$i->id;
                return $i;
            })->toArray();
        }
        elseif($level==2)
        {
            $city_ids_arr=[$pid];
        }
        $county_ids_arr=self::whereIn('pid',$city_ids_arr)->select('id')->get()
            ->transform(function ($i,$k)
            {
                $i=$i->id;
                return $i;
            })->toArray();
        return $county_ids_arr;
    }

    public static function setJsonFile() {
        if (!$sprovince = Redis::get('p:province')) {
            $provinces = self::where('level', 1)
                ->select(self::getField())
                ->get()->toArray();
            Redis::set('p:province',json_encode($provinces));
        } else {
            $provinces = json_decode($sprovince, true);
        }

        if (!$ccity = Redis::get('c:city')) {
            $citys = self::where('level', 2)
                ->select(self::getField())
                ->get()->toArray();
            Redis::set('c:city', json_encode($citys));
        } else {
            $citys = json_decode($ccity, true);
        }

        if (!$ycountry = Redis::get('y:country')) {
            $countrys = self::where('level', 3)
                ->select(self::getField())
                ->get()->toArray();
            Redis::set('y:country', json_encode($countrys));
        } else {
            $countrys = json_decode($ycountry, true);
        }

        $arr = [];
        foreach ($provinces as $key => $province) {
            $arr[$province['id']] = [
                'level' => $province['level'],
                'id' => $province['id'],
                'name' => $province['name'],
                'sname' => $province['sname'],
            ];
        }

        $pcityId = [];
        foreach ($citys as $city) {
            $pcityId[$city['id']] = $city['pid'];
            $arr[$city['pid']]['children'][$city['id']] = [
                'level' => 2,
                'id' => $city['id'],
                'pid' => $city['pid'],
                'name' => $city['name'],
                'sname' => $city['sname'],
            ];
        }

        foreach ($countrys as $country) {
            $arr[$pcityId[$country['pid']]]['children'][$country['pid']]['children'][$country['id']] = [
                'level' => 3,
                'id' => $country['id'],
                'pid' => $country['pid'],
                'name' => $country['name'],
                'sname' => $country['sname'],
            ];
        }

        file_put_contents('area_1.json', json_encode($arr));

        $tmp = [];
        foreach ($arr as $key => $province) {
            $cityTmp = [];
            if (!isset($province['children'])) continue;
            foreach ($province['children'] as $city) {
                if (!isset($city['children'])) continue;
                $city['children'] = array_values($city['children']);
                $cityTmp[] = $city;
            }
            $province['children'] = $cityTmp;
            $tmp[] = $province;
        }
//        echo json_encode($tmp);die;
        file_put_contents('area_2.json', json_encode($tmp));dd('end2....');


    }
}
